home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / c_toolbx.arc / BT_CACHE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1988-03-30  |  1.7 KB  |  67 lines

  1. /*  bt_cache.c - cache index blocks to cut Disk I/O */
  2. /*  this version handles only a single open index file */
  3. #include   "stdio.h"
  4. #include   "btree.h"
  5. #include   "bt_macro.h"
  6.  
  7. extern     IX_DESC    *pci ;    /* refers to index descriptor */
  8.                 /* for current function call  */
  9.  
  10. int  init_cache()        /* initilize blockio cache */
  11.   {
  12.      int   l ;
  13.  
  14.      for(l=0 ;l < MAX_LEVELS ; l=l+1 )
  15.     {  pci->cache[l].brec = NULLREC ; /* nothing in memory yet */
  16.     }
  17.   }
  18.  
  19. int  chk_cache(l,r)        /* check cache for an index block */
  20.   int    l ;            /* index level for this block */
  21.   RECPOS   r ;            /* block's location in index file */
  22.   {
  23.     BLOCK  *pb ;
  24.  
  25.      if( pci->cache[l].brec != r )
  26.     return( 0 ) ;
  27.      return( 1 ) ;
  28.   }
  29.  
  30. int  get_cache(l,r,to)        /* get a block from the cache */
  31.   int    l ;            /* index level for this block */
  32.   RECPOS   r ;            /* block's location in index file */
  33.   BLOCK *to ;            /* if found, copy it here */
  34.   {
  35.      BLOCK *pb ;
  36.  
  37.      if( pci->cache[l].brec != r )
  38.     return( 0 ) ;
  39.      pb = & pci->cache[l] ;
  40.      mover(to,pb, sizeof(BLOCK) ) ;    /* copy it */
  41.      pb->brec = r ;
  42.      return( 1) ;
  43.   }
  44.  
  45. int  put_cache(l,pb)        /* write an index block back to file */
  46.   int    l ;
  47.   BLOCK *pb ;            /* address of index block */
  48.   {
  49.      BLOCK *to ;
  50.  
  51.      to = & pci->cache[l] ;
  52.      mover(to,pb, sizeof(BLOCK) ) ;    /* copy whole block */
  53.   }
  54.  
  55. int  scrub_cache(r)        /* remove a block from the cache */
  56.   RECPOS   r ;            /* the block's file position */
  57.   {                /* = RECPOS scrubs all levels */
  58.      int   l ;
  59.  
  60.      for(l=0 ; l < pci->dx.nl ; l=l+1) /* search the cache */
  61.     {  if( (r == NULLREC) || ( r == pci->cache[l].brec) )
  62.        pci->cache[l].brec = NULLREC ;
  63.     }
  64.   }
  65.  
  66.  
  67.